﻿using LH.Model.PU;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using fuzhu;
using LH.Model.ST;
using System.Security.AccessControl; 
using System.Xml.Linq;
using XXX.Common;
using XXX.Bo;
using ADODB;
using XXX.Models;

namespace LH.U8API.Bus
{
   public static class HuaJin_GetInfo
    {
          

        public static string GetST01ByPOInfo(ST_Rdrecord01 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {
                string cPosId = rdrecord.bodies[i].iposid;
                string cInvCode = rdrecord.bodies[i].cinvcode;
                //增加表头及表体自定义项,部门根据最新的业务员取

                string sql = string.Format(@"SELECT a.cInvCode,b.cComUnitCode,b.cGroupCode,a.cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,
  po.cPOID, po.cPersonCode,po.cVenCode,po.cPTCode,po.cexch_name,po.iTaxRate,po.nflat,po.cBusType, 
a.bGsp,a.bTaxCost,a.iPerTaxRate, a.iTaxPrice,b.cinvdefine2,b.bfree1,b.igrouptype,binvbatch,b.bInvQuality,
po.cdefine1,po.cdefine2,po.cdefine3,po.cdefine4,po.cdefine5,po.cdefine6,po.cdefine7,po.cdefine8,po.cdefine9,po.cdefine10,
po.cdefine11,po.cdefine12,po.cdefine13,po.cdefine14,po.cdefine15,po.cdefine16,
 a.cdefine22, a.cdefine23, a.cdefine24, a.cdefine25, a.cdefine26, a.cdefine27, a.cdefine28, a.cdefine29, a.cdefine30, a.cdefine31, a.cdefine32,
  a.cdefine33, a.cdefine34, a.cdefine35, a.cdefine36, a.cDefine37,a.cItem_class,a.cItemCode,a.cItemName ,fitem.citem_name,
  a.SoType ,a.SoDId ,a.csocode,po.cMemo,a.cbMemo,p.cDepCode 
FROM dbo.PO_Podetails a with(nolock)
   INNER JOIN dbo.PO_Pomain po with(nolock) ON a.POID=po.POID
  INNER JOIN dbo.Inventory b with(nolock) ON a.cInvCode=b.cInvCode
  INNER JOIN inventory_sub d with(nolock) ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c with(nolock) ON a.cUnitID=c.cComunitCode
  left join person p on po.cPersonCode = p.cpersoncode
 left join fitem on a.cItem_class = fitem.cItem_class 
  WHERE a.id = '{0}' ", cPosId);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行采购订单ID不正确，查询不到采购订单", (i + 1).ToString());
                }
                else
                {
                    if (DbHelper.GetDbString(dt.Rows[0]["cInvCode"]) != rdrecord.bodies[i].cinvcode)
                    {
                        return string.Format("第{0}行订单物料和{1}不一致", (i + 1).ToString(), DbHelper.GetDbString(dt.Rows[0]["cInvCode"]));
                    }
                    //写表头信息
                    if (i == 0)
                    {
                        rdrecord.head.cptcode = DbHelper.GetDbString(dt.Rows[0]["cPTCode"]);
                        rdrecord.head.cvencode = DbHelper.GetDbString(dt.Rows[0]["cVenCode"]);
                        //没有取到部门的情况下，使用这个部门
                        if (string.IsNullOrEmpty(rdrecord.head.cdepcode))
                        {
                            rdrecord.head.cdepcode = DbHelper.GetDbString(dt.Rows[0]["cDepCode"]);
                            rdrecord.head.cpersoncode = DbHelper.GetDbString(dt.Rows[0]["cPersonCode"]);
                        }

                        rdrecord.head.cordercode = DbHelper.GetDbString(dt.Rows[0]["cPOID"]);
                        rdrecord.head.cexch_name = DbHelper.GetDbString(dt.Rows[0]["cexch_name"]);
                        rdrecord.head.itaxrate = DbHelper.GetDbString(dt.Rows[0]["iTaxRate"]);
                        rdrecord.head.iexchrate = DbHelper.GetDbString(dt.Rows[0]["nflat"]);
                        rdrecord.head.cbustype = DbHelper.GetDbString(dt.Rows[0]["cBusType"]);


                        rdrecord.head.cdefine1 = DbHelper.GetDbString(dt.Rows[0]["cdefine1"]);
                        rdrecord.head.cdefine2 = DbHelper.GetDbString(dt.Rows[0]["cdefine2"]);
                        rdrecord.head.cdefine3 = DbHelper.GetDbString(dt.Rows[0]["cdefine3"]);
                        rdrecord.head.cdefine4 = DbHelper.GetDbString(dt.Rows[0]["cdefine4"]);
                        rdrecord.head.cdefine5 = DbHelper.GetDbString(dt.Rows[0]["cdefine5"]);
                        rdrecord.head.cdefine6 = DbHelper.GetDbString(dt.Rows[0]["cdefine6"]);
                        rdrecord.head.cdefine7 = DbHelper.GetDbString(dt.Rows[0]["cdefine7"]);
                        rdrecord.head.cdefine8 = DbHelper.GetDbString(dt.Rows[0]["cdefine8"]);
                        rdrecord.head.cdefine9 = DbHelper.GetDbString(dt.Rows[0]["cdefine9"]);
                        rdrecord.head.cdefine10 = DbHelper.GetDbString(dt.Rows[0]["cdefine10"]);
                        rdrecord.head.cdefine11 = DbHelper.GetDbString(dt.Rows[0]["cdefine11"]);
                        rdrecord.head.cdefine12 = DbHelper.GetDbString(dt.Rows[0]["cdefine12"]);
                        rdrecord.head.cdefine13 = DbHelper.GetDbString(dt.Rows[0]["cdefine13"]);
                        rdrecord.head.cdefine14 = DbHelper.GetDbString(dt.Rows[0]["cdefine14"]);
                        rdrecord.head.cdefine15 = DbHelper.GetDbString(dt.Rows[0]["cdefine15"]);
                        rdrecord.head.cdefine16 = DbHelper.GetDbString(dt.Rows[0]["cdefine16"]);
                        //没有传备注的，取订单的备注
                        if (string.IsNullOrEmpty(rdrecord.head.cmemo))
                        {
                            rdrecord.head.cmemo = DbHelper.GetDbString(dt.Rows[0]["cMemo"]);
                        }
                        


                    }
                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);
                    rdrecord.bodies[i].btaxcost = DbHelper.GetDbString(dt.Rows[0]["bTaxCost"]);
                    //使用表体税率
                    rdrecord.bodies[i].itaxrate = DbHelper.GetDbString(dt.Rows[0]["iPerTaxRate"]);
                    //rdrecord.bodies[i]. = DbHelper.GetDbString(dt.Rows[0]["cGroupCode"]);
                 
                    //rdrecord.bodies[i].iposid = DbHelper.GetDbString(dt.Rows[0]["iposid"]);
                    rdrecord.bodies[i].cpoid = DbHelper.GetDbString(dt.Rows[0]["cPOID"]);

                    //处理单价  
                    rdrecord.bodies[i].ioritaxcost = DbHelper.GetDbString(dt.Rows[0]["iTaxPrice"]);
                    //写其他金额
                    decimal dSum = Math.Round(DbHelper.GetDbdecimal(dt.Rows[0]["iTaxPrice"]) * DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity), 2);
                    decimal dMoney = Math.Round(dSum / (1 + DbHelper.GetDbdecimal(dt.Rows[0]["iPerTaxRate"]) / 100), 2);
                    decimal dTax = dSum - dMoney;
                    decimal ioricost = dMoney / DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity);
                    decimal dExch = DbHelper.GetDbdecimal(dt.Rows[0]["nflat"]);

                    rdrecord.bodies[i].iorisum = dSum.ToString("0.00");
                    rdrecord.bodies[i].iorimoney = dMoney.ToString("0.00");
                    rdrecord.bodies[i].ioritaxprice = (dSum - dMoney).ToString("0.00");
                    rdrecord.bodies[i].iprice = (dMoney * dExch).ToString("0.00");
                    rdrecord.bodies[i].iaprice = (dMoney * dExch).ToString("0.00");
                    rdrecord.bodies[i].facost = (ioricost * dExch).ToString("0.000000");
                    rdrecord.bodies[i].isum = (dSum * dExch).ToString("0.00");
                    //rdrecord.bodies[i].imoney = (dMoney * dExch).ToString("0.00");  //累计结算金额，不能写
                    rdrecord.bodies[i].itaxprice = ((dSum - dMoney) * dExch).ToString("0.00");
                    rdrecord.bodies[i].ioricost = ioricost.ToString("0.000000");
                    rdrecord.bodies[i].iunitcost = (ioricost * dExch).ToString("0.000000");
                    string iGrouptype = DbHelper.GetDbString(dt.Rows[0]["igrouptype"]);
                    //固定换算率和浮动换算率，都是需要件数和辅助计量单位，换算率
                    if (iGrouptype == "1" || iGrouptype == "2")
                    {
                        //固定换算
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                        {
                            rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                        }
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cUnitID"]);
                        rdrecord.bodies[i].inum = DbHelper.GetDbString(DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity) / DbHelper.GetDbdecimal(dt.Rows[0]["iChangRate"]));

                    }

                    //有批次管理的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["binvbatch"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].cbatch))
                        {
                            return rdrecord.bodies[i].cinvcode + "批次没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].cbatch = "";
                    }
                    //有保质期的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["bInvQuality"]) == 1)
                    {
                         if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].dvdate))
                        {
                            return rdrecord.bodies[i].cinvcode + "失效没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].dvdate = "";
                    }
                    //项目
                    //检查项目是否已关闭
                    if (DbHelper.GetDbString(dt.Rows[0]["cItem_class"]) == "00")
                    {
                        sql = string.Format(@"select bclose from fitemss00 where citemcode='{0}'", DbHelper.GetDbString(dt.Rows[0]["cItemCode"]));
                        object obj = DbHelper2.GetSingle(sql,CanShu.conStr);
                        if (obj !=null)
                        {
 
                            //项目关闭了，只填写大类，不填写明细
                            if (DbHelper.GetDbInt(obj)==1)
                            {
                                //rdrecord.bodies[i].citem_class = DbHelper.GetDbString(dt.Rows[0]["cItem_class"]);
                                //rdrecord.bodies[i].citemcname = DbHelper.GetDbString(dt.Rows[0]["citem_name"]);
                                rdrecord.bodies[i].cbatchproperty8 = "订单项目已关闭";


                            }
                            else
                            {
                                //研发项目赋值
                                rdrecord.bodies[i].citem_class = DbHelper.GetDbString(dt.Rows[0]["cItem_class"]);
                                rdrecord.bodies[i].citemcname = DbHelper.GetDbString(dt.Rows[0]["citem_name"]);
                                rdrecord.bodies[i].cname = DbHelper.GetDbString(dt.Rows[0]["cItemName"]);
                                rdrecord.bodies[i].citemcode = DbHelper.GetDbString(dt.Rows[0]["cItemCode"]);
                            }
                        }
                    }

                   


                    

                    //表体备注
                    rdrecord.bodies[i].cbmemo = DbHelper.GetDbString(dt.Rows[0]["cbMemo"]);
                    //表体自定义项 
                    rdrecord.bodies[i].cdefine22 = DbHelper.GetDbString(dt.Rows[0]["cdefine22"]);
                    rdrecord.bodies[i].cdefine23 = DbHelper.GetDbString(dt.Rows[0]["cdefine23"]);
                    rdrecord.bodies[i].cdefine24 = DbHelper.GetDbString(dt.Rows[0]["cdefine24"]);
                    rdrecord.bodies[i].cdefine25 = DbHelper.GetDbString(dt.Rows[0]["cdefine25"]);
                    rdrecord.bodies[i].cdefine26 = DbHelper.GetDbString(dt.Rows[0]["cdefine26"]);
                    rdrecord.bodies[i].cdefine27 = DbHelper.GetDbString(dt.Rows[0]["cdefine27"]);
                    rdrecord.bodies[i].cdefine28 = DbHelper.GetDbString(dt.Rows[0]["cdefine28"]);
                    rdrecord.bodies[i].cdefine29 = DbHelper.GetDbString(dt.Rows[0]["cdefine29"]);
                    rdrecord.bodies[i].cdefine30 = DbHelper.GetDbString(dt.Rows[0]["cdefine30"]);
                    rdrecord.bodies[i].cdefine31 = DbHelper.GetDbString(dt.Rows[0]["cdefine31"]);
                    rdrecord.bodies[i].cdefine32 = DbHelper.GetDbString(dt.Rows[0]["cdefine32"]);
                    rdrecord.bodies[i].cdefine33 = DbHelper.GetDbString(dt.Rows[0]["cdefine33"]);
                    rdrecord.bodies[i].cdefine34 = DbHelper.GetDbString(dt.Rows[0]["cdefine34"]);
                    rdrecord.bodies[i].cdefine35 = DbHelper.GetDbString(dt.Rows[0]["cdefine35"]);
                    rdrecord.bodies[i].cdefine36 = DbHelper.GetDbString(dt.Rows[0]["cdefine36"]);
                    rdrecord.bodies[i].cdefine37 = DbHelper.GetDbString(dt.Rows[0]["cdefine37"]);

                }
            }
            return "ok";
        }

        public static string GetST08Info(ST_Rdrecord08 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {


                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT b.cInvCode,b.cComUnitCode,b.cGroupCode,
b.cSTComUnitCode AS cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,b.igrouptype,binvbatch,b.bInvQuality
FROM   dbo.Inventory b  
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON b.cSTComUnitCode=c.cComunitCode
  WHERE b.cinvcode  = '{0}' ", cInvCode);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行存货编码ID不正确", (i + 1).ToString());
                }
                else
                {

                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);
 
                    string iGrouptype = DbHelper.GetDbString(dt.Rows[0]["igrouptype"]);
                    //固定换算率和浮动换算率，都是需要件数和辅助计量单位，换算率
                    if (iGrouptype == "1" || iGrouptype == "2")
                    {
                        //固定换算
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                        {
                            rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                        }
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cUnitID"]);
                        rdrecord.bodies[i].inum = DbHelper.GetDbString(DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity) / DbHelper.GetDbdecimal(dt.Rows[0]["iChangRate"]));

                    }

                    //有批次管理的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["binvbatch"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].cbatch))
                        {
                            return rdrecord.bodies[i].cinvcode + "批次没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].cbatch = "";
                    }
                    //有保质期的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["bInvQuality"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].dvdate))
                        {
                            return rdrecord.bodies[i].cinvcode + "失效没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].dvdate = "";
                    }

                }
            }
            return "ok";
        }

        public static string GetST09Info(ST_Rdrecord09 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {


                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT b.cInvCode,b.cComUnitCode,b.cGroupCode,
b.cSTComUnitCode AS cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu, b.igrouptype,binvbatch,b.bInvQuality
FROM   dbo.Inventory b  
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON b.cSTComUnitCode=c.cComunitCode
  WHERE b.cinvcode  = '{0}' ", cInvCode);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行存货编码ID不正确", (i + 1).ToString());
                }
                else
                {

                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);

                    string iGrouptype = DbHelper.GetDbString(dt.Rows[0]["igrouptype"]);
                    //固定换算率和浮动换算率，都是需要件数和辅助计量单位，换算率
                    if (iGrouptype == "1" || iGrouptype == "2")
                    {
                        //固定换算
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                        {
                            rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                        }
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cUnitID"]);
                        rdrecord.bodies[i].inum = DbHelper.GetDbString(DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity) / DbHelper.GetDbdecimal(dt.Rows[0]["iChangRate"]));

                    }
                    //有批次管理的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["binvbatch"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].cbatch))
                        {
                            return rdrecord.bodies[i].cinvcode + "批次没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].cbatch = "";
                    }
                    //有保质期的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["bInvQuality"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].dvdate))
                        {
                            return rdrecord.bodies[i].cinvcode + "失效没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].dvdate = "";
                    }

                }
            }
            return "ok";
        }

        public static string GetTransVouchInfo(LH.Model.ST.ST_TransVouch rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }
            //取调拨申请单的数据
            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {
                string cInvCode = rdrecord.bodies[i].cInvCode;
                string sql = string.Format(@"SELECT b.cInvCode,b.cComUnitCode,b.cGroupCode,
c.cComunitCode,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,b.igrouptype,binvbatch,b.bInvQuality
FROM dbo.Inventory b  
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON b.cSTComUnitCode=c.cComunitCode
  WHERE b.cInvCode = '{0}' ", cInvCode);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行物料编码不正确", (i + 1).ToString());
                }
                else
                {
                    string iGrouptype = DbHelper.GetDbString(dt.Rows[0]["igrouptype"]);
                    //固定换算率和浮动换算率，都是需要件数和辅助计量单位，换算率
                    if (iGrouptype == "1" || iGrouptype == "2")
                    {
                        //固定换算
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                        {
                            rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                        }
                        rdrecord.bodies[i].cAssUnit = DbHelper.GetDbString(dt.Rows[0]["cUnitID"]);
                        rdrecord.bodies[i].iTVNum = DbHelper.GetDbString(DbHelper.GetDbdecimal(rdrecord.bodies[i].iTVQuantity) / DbHelper.GetDbdecimal(dt.Rows[0]["iChangRate"]));

                    }

                    //有批次管理的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["binvbatch"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].cTVBatch))
                        {
                            return rdrecord.bodies[i].cInvCode + "批次没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].cTVBatch = "";
                    }
                    //有保质期的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["bInvQuality"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].dDisDate))
                        {
                            return rdrecord.bodies[i].cInvCode + "失效没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].dDisDate = "";
                    }

                }
            }
            return "ok";
        }


        //public static string GetMoInfo_FeiBiao(HuaJinMorder model, string cConstr, string bupdate)
        //{
        //    string sql = "";
        //    if (bupdate == "0")
        //    {
        //        //取生产部门  PSNO，工段，BUMPING(前道）/ DP(前道）/ ASSY(后道）
        //        if (!string.IsNullOrEmpty(model.PSNO))
        //        {
        //            if (model.PSNO.ToUpper() == "BUMPING" || model.PSNO.ToUpper() == "DP")
        //            {
        //                model.cDepCode = CanShu.DepMoQian;
        //            }
        //            else if (model.PSNO.ToUpper() == "ASSY")
        //            {
        //                model.cDepCode = CanShu.DepMoHou;
        //            }
        //        }

               
        //    }

        //    //查询销售订单id和行号
        //    if (!string.IsNullOrEmpty(model.OrderDId))
        //    {
        //        sql = string.Format(@"select a.csocode,b.irowno,b.isosid,a.ccuscode from so_somain a, SO_SODetails b where  a.id = b.id and b.autoid = '{0}'", model.OrderDId);
        //        DataTable dtid = DbHelper2.ExecuteTable(sql, CanShu.conStr);
        //        if (dtid != null && dtid.Rows.Count > 0)
        //        {
        //            model.OrderCode = DbHelper.GetDbString(dtid.Rows[0]["csocode"]);
        //            model.OrderType = "1";
        //            model.OrderSeq = DbHelper.GetDbString(dtid.Rows[0]["irowno"]);
        //            model.CustCode = DbHelper.GetDbString(dtid.Rows[0]["ccuscode"]);
        //            //重写orderdid = isosid
        //            model.OrderDId = DbHelper.GetDbString(dtid.Rows[0]["isosid"]);
        //        }
        //    }
        //    //取partid
        //    RetModel2 ret = InvService.GetPartidCode(model.cInvCode);
        //    if (ret.code == 200)
        //    {
        //        model.Partid = DbHelper.GetDbString(ret.data);
        //    }
        //    else
        //    {
        //        throw new Exception(string.Format("产品编码{0}在U8内有错误：", model.cInvCode) + ret.message);
        //    }

             
        //    //生产类型处理
        //    if (!string.IsNullOrEmpty(model.cMoType))
        //    {
        //        //验机工单或者dummy工单,传U8，变成研发工单
        //        //20240923  更改成代码

        //        //if (model.cMoType == "验机工单" || model.cMoType == "dummy工单")
        //        //{
        //        //    model.cMoType = "研发工单";
        //        //}

        //        //sql = string.Format(@"select MoTypeId from mom_motype where Description = '{0}'", model.cMoType);
        //        //按工单类别编码取数
        //        sql = string.Format(@"select MoTypeId from mom_motype where MotypeCode = '{0}'", model.cMoType);
        //        string MoTypeId = DbHelper.GetDbString(DbHelper2.ExecuteScalar(sql, CanShu.conStr));
        //        if (!string.IsNullOrEmpty(MoTypeId))
        //        {
        //            model.MoTypeId = MoTypeId;
        //        }
        //        //自定义批次类型信息
        //        //moclass  生产订单类型： 1标准，2非标准。 返工工单，60
        //        //全部是非标工单
        //        model.MoClass = "2";
        //        if (model.cMoType == "60")
        //        {
        //            model.Define32 = "返工批次"; 
        //        }
        //        else
        //        {
        //            model.Define32 = "正常批次"; 
        //        }
        //    }
        //    return "ok";
        //}


        //public static string GetMoInfo(HuaJinMorder model, string cConstr,string bupdate)
        //{
        //    string sql = "";
        //    if (bupdate== "0")
        //    {
        //        //取生产部门  PSNO，工段，BUMPING(前道）/ DP(前道）/ ASSY(后道）
        //      if (!string.IsNullOrEmpty(model.PSNO))
        //        {
        //            if (model.PSNO.ToUpper() == "BUMPING" || model.PSNO.ToUpper() == "DP")
        //            {
        //                model.cDepCode = CanShu.DepMoQian;
        //            }
        //            else if (model.PSNO.ToUpper() == "ASSY")
        //            {
        //                model.cDepCode = CanShu.DepMoHou;
        //            }
        //        }
              
        //        //string cMaker = model.cMaker;
        //        //sql = string.Format(@"select  cDept_num from hr_hi_person where cpsn_name = '{0}' and rEmployState ='10'", model.cMaker);
        //        //string cDept = DbHelper.GetDbString(DbHelper2.ExecuteScalar(sql, CanShu.conStr));
        //        //if (string.IsNullOrEmpty(cDept))
        //        //{
        //        //    throw new Exception(string.Format("当前人员{0}无法取到对应部门，无法保存", model.cMaker));
        //        //}
        //        //else
        //        //{
        //        //    model.cDepCode = cDept;
        //        //}
        //    }
            
        //    //查询销售订单id和行号
        //    if (!string.IsNullOrEmpty(model.OrderDId))
        //    {
        //        sql = string.Format(@"select a.csocode,b.irowno,b.isosid,a.ccuscode from so_somain a, SO_SODetails b where  a.id = b.id and b.autoid = '{0}'", model.OrderDId);
        //        DataTable dtid = DbHelper2.ExecuteTable(sql, CanShu.conStr);
        //        if (dtid != null && dtid.Rows.Count > 0)
        //        {
        //            model.OrderCode = DbHelper.GetDbString(dtid.Rows[0]["csocode"]);
        //            model.OrderType = "1";
        //            model.OrderSeq = DbHelper.GetDbString(dtid.Rows[0]["irowno"]);
        //            model.CustCode = DbHelper.GetDbString(dtid.Rows[0]["ccuscode"]);
        //        }
        //    }
        //    //取partid
        //    RetModel2 ret = InvService.GetPartidCode(model.cInvCode);
        //    if (ret.code == 200)
        //    {
        //        model.Partid = DbHelper.GetDbString(ret.data);
        //    }
        //    else
        //    {
        //        throw new Exception(string.Format("产品编码{0}在U8内有错误：", model.cInvCode) + ret.message);
        //    }
         
        //    for (int i = 0; i < model.itemList.Count; i++)
        //    {
        //        RetModel2 retchild = InvService.GetPartidCode(model.itemList[i].cChildCode);
        //        if (retchild.code == 200)
        //        {
        //            model.itemList[i].Componentid = DbHelper.GetDbString(retchild.data);
        //            //检查是否有辅助计量
        //            DataTable dtaux = InvService.GetAuxCode(model.itemList[i].cChildCode);
        //            //计算总用量
        //            Decimal dQty = Math.Round(DbHelper.GetDbdecimal(model.itemList[i].dBaseQtyN) * DbHelper.GetDbdecimal(model.iQuantity), 6);
        //            model.itemList[i].dQty = dQty.ToString("0.000000");
        //            if (dtaux != null && dtaux.Rows.Count > 0)
        //            {
        //                model.itemList[i].AuxUnitCode = DbHelper.GetDbString(dtaux.Rows[0]["cSTComUnitCode"]);
        //                decimal dChangerate = DbHelper.GetDbdecimal(model.itemList[i].ChangeRate);
        //                model.itemList[i].ChangeRate = DbHelper.GetDbString(dChangerate);
        //                if (dChangerate != 0)
        //                {
        //                    model.itemList[i].AuxBaseQtyN = (DbHelper.GetDbdecimal(model.itemList[i].dBaseQtyN) / dChangerate).ToString("0.000000");
        //                    model.itemList[i].AuxQty = (dQty / dChangerate).ToString("0.000000");
        //                }

        //            }

        //        }
        //        else
        //        {
        //            throw new Exception(string.Format("子件编码{0}在U8内有错误：", model.itemList[i].cChildCode) + ret.message);
        //        }

        //        //写替代料的partid
        //        if (model.itemList[i].subList != null)
        //        {
        //            for (int m = 0; m < model.itemList[i].subList.Count; m++)
        //            {
        //                RetModel2 retchild2 = InvService.GetPartidCode(model.itemList[i].subList[m].cSubCode);
        //                if (retchild2.code == 200)
        //                {
        //                    model.itemList[i].subList[m].SubPartid = DbHelper.GetDbString(retchild2.data);
        //                }
        //                else
        //                {
        //                    throw new Exception(string.Format("替代料{0}在U8内有错误：", model.itemList[i].subList[m].cSubCode) + ret.message);
        //                }
        //            }
        //        }
        //    }
        //    //生产类型处理
        //    if (!string.IsNullOrEmpty(model.cMoType))
        //    {
        //        //验机工单或者dummy工单,传U8，变成研发工单
        //        //20240923  更改成代码

        //        //if (model.cMoType == "验机工单" || model.cMoType == "dummy工单")
        //        //{
        //        //    model.cMoType = "研发工单";
        //        //}

        //        //sql = string.Format(@"select MoTypeId from mom_motype where Description = '{0}'", model.cMoType);
        //        //按工单类别编码取数
        //        sql = string.Format(@"select MoTypeId from mom_motype where MotypeCode = '{0}'", model.cMoType);
        //        string MoTypeId = DbHelper.GetDbString(DbHelper2.ExecuteScalar(sql, CanShu.conStr));
        //        if (!string.IsNullOrEmpty(MoTypeId))
        //        {
        //            model.MoTypeId = MoTypeId;
        //        }
        //        //自定义批次类型信息
        //        //moclass  生产订单类型： 1标准，2非标准。 返工工单，60
        //        if (model.cMoType == "60")
        //        {
        //            model.Define32 = "返工批次";
        //            model.MoClass = "2";
        //        }
        //        else
        //        {
        //            model.Define32 = "正常批次";
        //            model.MoClass = "1";
        //        } 
        //    }
        //    return "ok";
        //}

        public static string GetST11ByMoInfo(ST_Rdrecord11 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {
                //根据生产订单和存货编码，查询出对应生产订单子件的ID
                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT a.InvCode,a.AuxUnitCode,a.ChangeRate,b.cGroupCode,b.iGroupType, b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,
    b.bfree1,b.igrouptype,binvbatch,b.bInvQuality,mo.MDeptCode, mo.OrderType,mo.OrderCode,mo.OrderDId,mo.OrderSeq,
	a.AllocateId,a.MoDId ,define31,mo.SortSeq,mo.InvCode as moInvcode
FROM dbo.mom_moallocate a with(nolock)
   INNER JOIN dbo.mom_orderdetail mo with(nolock) ON a.MoDId=mo.MoDId
   inner join mom_order morder  with(nolock) on mo.MoId = morder.MoId
  INNER JOIN dbo.Inventory b with(nolock) ON a.InvCode=b.cInvCode 
  inner join Inventory_sub d with(nolock) ON a.InvCode=b.cInvCode 
  WHERE a.invcode = '{0}' and morder.mocode='{1}'  ", cInvCode,rdrecord.head.cmpocode);
                //如果有多行的情况下，再检查 工站。原因： 下料的时候没有工序 
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    //材料找不到的情况下，查找替代料是否有
                    sql = string.Format(@"SELECT a.InvCode, b.cSTComUnitCode as AuxUnitCode,com.iChangRate as ChangeRate,b.cGroupCode,b.iGroupType, b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,
    b.bfree1,b.igrouptype,binvbatch,b.bInvQuality,mo.MDeptCode, mo.OrderType,mo.OrderCode,mo.OrderDId,mo.OrderSeq,
	a.AllocateId,a.MoDId ,sub.MoallocateSubId,mo.SortSeq,mo.InvCode as moInvcode
FROM dbo.mom_moallocate a with(nolock)
   INNER JOIN dbo.mom_orderdetail mo with(nolock) ON a.MoDId=mo.MoDId
   inner join mom_order morder with(nolock) on mo.MoId = morder.MoId
   inner join mom_moallocatesub sub  with(nolock) on a.AllocateId = sub.AllocateId
   inner join bas_part part with(nolock) on sub.PartId = part.PartId
  INNER JOIN dbo.Inventory b with(nolock) ON part.InvCode=b.cInvCode 
   inner join Inventory_sub d with(nolock) ON a.InvCode=b.cInvCode 
  left join ComputationUnit com with(nolock) on b.cSTComUnitCode=com.cComunitCode
  WHERE b.cinvcode = '{0}' and morder.mocode='{1}'  ", cInvCode, rdrecord.head.cmpocode);
                    DataTable dtsub = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                    if (dtsub.Rows.Count > 0)
                    {
                        return string.Format("第{0}行存货编码ID不正确", (i + 1).ToString());
                    }
                    else
                    {
                        #region  替代料处理
                        //默认替代料不重复
                        if (i == 0)
                        {
                            //部门取生产部门
                            rdrecord.head.cdepcode = DbHelper.GetDbString(dtsub.Rows[0]["MDeptCode"]);
                            rdrecord.head.iproorderid = DbHelper.GetDbString(dt.Rows[0]["MoDId"]);
                            //rdrecord.head.cpersoncode = DbHelper.GetDbString(dt.Rows[0]["cPersonCode"]);
                        }
                        int m = 0;
                        //多行的情况，取工站相同的
                        //只有1行的，取第一行。
                        for (int k = 0; k < dtsub.Rows.Count; k++)
                        {
                            if (rdrecord.bodies[i].cdefine31 == DbHelper.GetDbString(dtsub.Rows[k]["define31"]))
                            {
                                m = k; 
                                break;
                            }
                        }
                        rdrecord.bodies[i].impoids = DbHelper.GetDbString(dtsub.Rows[m]["AllocateId"]);
                        rdrecord.bodies[i].imoallocatesubid = DbHelper.GetDbString(dtsub.Rows[m]["MoallocateSubId"]);
                        rdrecord.bodies[i].cmocode = rdrecord.head.cmpocode;
                        rdrecord.bodies[i].imoseq = DbHelper.GetDbString(dtsub.Rows[m]["SortSeq"]);
                        rdrecord.bodies[i].invcode = DbHelper.GetDbString(dtsub.Rows[m]["moInvcode"]);
                        rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dtsub.Rows[m]["cInvCode"]);
                        rdrecord.bodies[i].iordertype = DbHelper.GetDbString(dt.Rows[m]["OrderType"]);
                        rdrecord.bodies[i].iordercode = DbHelper.GetDbString(dt.Rows[m]["OrderCode"]);
                        rdrecord.bodies[i].iorderdid = DbHelper.GetDbString(dt.Rows[m]["OrderDId"]);
                        rdrecord.bodies[i].iorderseq = DbHelper.GetDbString(dt.Rows[m]["OrderSeq"]);


                       
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dtsub.Rows[0]["AuxUnitCode"])))
                        {
                            rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dtsub.Rows[0]["AuxUnitCode"]);

                        }

                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dtsub.Rows[0]["ChangeRate"])))
                        {
                            rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dtsub.Rows[0]["ChangeRate"]);
                            rdrecord.bodies[i].inum = (DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity) / DbHelper.GetDbdecimal(dtsub.Rows[0]["ChangeRate"])).ToString("0.000000");
                            rdrecord.bodies[i].innum = rdrecord.bodies[i].inum;
                        }
                        //有批次管理的填写，没有的不写
                        if (DbHelper.GetDbInt(dt.Rows[0]["binvbatch"]) == 1)
                        {
                            if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].cbatch))
                            {
                                return rdrecord.bodies[i].cinvcode + "批次没有填写";
                            }

                        }
                        else
                        {
                            rdrecord.bodies[i].cbatch = "";
                        }
                        //有保质期的填写，没有的不写
                        if (DbHelper.GetDbInt(dt.Rows[0]["bInvQuality"]) == 1)
                        {
                            if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].dvdate))
                            {
                                return rdrecord.bodies[i].cinvcode + "失效没有填写";
                            }

                        }
                        else
                        {
                            rdrecord.bodies[i].dvdate = "";
                        }

                        #endregion
                    }

                   
                }
                else
                {
                   
                    if (i == 0)
                    {
                        //部门取生产部门
                        rdrecord.head.cdepcode = DbHelper.GetDbString(dt.Rows[0]["MDeptCode"]);
                        rdrecord.head.iproorderid = DbHelper.GetDbString(dt.Rows[0]["MoDId"]);
                        //rdrecord.head.cpersoncode = DbHelper.GetDbString(dt.Rows[0]["cPersonCode"]);
                    }
                    int m = 0;
                    //多行的情况，取工站相同的
                    //只有1行的，取第一行。
                        for (int k = 0; k < dt.Rows.Count; k++)
                        {
                            if (rdrecord.bodies[i].cdefine31 == DbHelper.GetDbString(dt.Rows[k]["define31"]))
                            {
                                m = k;
                               
                                break;
                            }
                        }
                    rdrecord.bodies[i].impoids = DbHelper.GetDbString(dt.Rows[m]["AllocateId"]);  
                    rdrecord.bodies[i].imoseq = DbHelper.GetDbString(dt.Rows[m]["SortSeq"]);
                    rdrecord.bodies[i].cmocode = rdrecord.head.cmpocode;
                    rdrecord.bodies[i].invcode = DbHelper.GetDbString(dt.Rows[m]["moInvcode"]);
                    rdrecord.bodies[i].iordertype = DbHelper.GetDbString(dt.Rows[m]["OrderType"]);
                    rdrecord.bodies[i].iordercode = DbHelper.GetDbString(dt.Rows[m]["OrderCode"]);
                    rdrecord.bodies[i].iorderdid = DbHelper.GetDbString(dt.Rows[m]["OrderDId"]);
                    rdrecord.bodies[i].iorderseq = DbHelper.GetDbString(dt.Rows[m]["OrderSeq"]);
                    ////没有匹配上的，取第一行

                    //如果有多行，再检查工站是否相同                    
                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);
                   
                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["AuxUnitCode"])))
                    {
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["AuxUnitCode"]);

                    }

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["ChangeRate"])))
                    {
                        rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["ChangeRate"]);
                        rdrecord.bodies[i].inum = (DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity) / DbHelper.GetDbdecimal(dt.Rows[0]["ChangeRate"])).ToString("0.000000");
                        rdrecord.bodies[i].innum = rdrecord.bodies[i].inum;
                    }
                    //有批次管理的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["binvbatch"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].cbatch))
                        {
                            return rdrecord.bodies[i].cinvcode + "批次没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].cbatch = "";
                    }
                    //有保质期的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["bInvQuality"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].dvdate))
                        {
                            return rdrecord.bodies[i].cinvcode + "失效没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].dvdate = "";
                    }
                    //项目
                    //rdrecord.bodies[i].citem_class = DbHelper.GetDbString(dt.Rows[0]["cItem_class"]);
                    //rdrecord.bodies[i].citemcname = DbHelper.GetDbString(dt.Rows[0]["citem_name"]);
                    //rdrecord.bodies[i].cname = DbHelper.GetDbString(dt.Rows[0]["cItemName"]);
                    //rdrecord.bodies[i].citemcode = DbHelper.GetDbString(dt.Rows[0]["cItemCode"]);


                }
            }
            return "ok";
        }
        //表外领料
        public static string GetST11ByMoInfo_BiaoWai(ST_Rdrecord11 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            string sql2 = string.Format(@"SELECT  mdeptcode,mo.MoTypeId,mo.define23 as cItemCode
FROM   dbo.mom_orderdetail mo with(nolock)  
   inner join mom_order morder  with(nolock) on mo.MoId = morder.MoId 
  WHERE  morder.mocode='{0}'  ", rdrecord.head.cmpocode);
            DataTable dtmo = DbHelper2.ExecuteTable(sql2, cConstr);
            if (dtmo == null || dtmo.Rows.Count == 0) 
            {
                return "U8中没有这个工单号"+ rdrecord.head.cmpocode;
            }
            else
            {
                rdrecord.head.cdepcode =DbHelper.GetDbString(dtmo.Rows[0]["mdeptcode"]);
            }
            

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {


                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT b.cInvCode,b.cComUnitCode,b.cGroupCode,
b.cSTComUnitCode AS cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,b.igrouptype,binvbatch,b.bInvQuality
FROM   dbo.Inventory b  
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON b.cSTComUnitCode=c.cComunitCode
  WHERE b.cinvcode  = '{0}' ", cInvCode);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行存货编码不正确", (i + 1).ToString());
                }
                else
                {

                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);

                    string iGrouptype = DbHelper.GetDbString(dt.Rows[0]["igrouptype"]);
                    //固定换算率和浮动换算率，都是需要件数和辅助计量单位，换算率
                    if (iGrouptype == "1" || iGrouptype == "2")
                    {
                        //固定换算
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                        {
                            rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                        }
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cUnitID"]);
                        rdrecord.bodies[i].inum = DbHelper.GetDbString(DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity) / DbHelper.GetDbdecimal(dt.Rows[0]["iChangRate"]));

                    }

                    //有批次管理的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["binvbatch"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].cbatch))
                        {
                            return rdrecord.bodies[i].cinvcode + "批次没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].cbatch = "";
                    }
                    //有保质期的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["bInvQuality"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].dvdate))
                        {
                            return rdrecord.bodies[i].cinvcode + "失效日期没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].dvdate = "";
                    }
                    //增加研发项目取值.MoTypeId = 1000000002， 项目大类默认00

                    if (DbHelper.GetDbString(dtmo.Rows[0]["MoTypeId"]) == "1000000002")
                    {
                        sql = string.Format(@"select bclose,cItemName from fitemss00 where citemcode='{0}'", DbHelper.GetDbString(dtmo.Rows[0]["cItemCode"]));
                        DataTable dtitem = DbHelper2.ExecuteTable(sql, CanShu.conStr);
                        if (dtitem.Rows.Count>0) 
                        {

                            //项目关闭了，只填写大类，不填写明细
                            if (DbHelper.GetDbInt(dtitem.Rows[0]["bclose"]) == 1)
                            {
                                //rdrecord.bodies[i].citem_class = DbHelper.GetDbString(dt.Rows[0]["cItem_class"]);
                                //rdrecord.bodies[i].citemcname = DbHelper.GetDbString(dt.Rows[0]["citem_name"]);
                                rdrecord.bodies[i].cbatchproperty8 = "订单项目已关闭"; 
                            }
                            else
                            {
                                //研发项目赋值
                                rdrecord.bodies[i].citem_class = "00";
                                rdrecord.bodies[i].citemcname = "研发项目";
                                rdrecord.bodies[i].cname = DbHelper.GetDbString(dtitem.Rows[0]["cItemName"]);
                                rdrecord.bodies[i].citemcode = DbHelper.GetDbString(dtmo.Rows[0]["cItemCode"]);
                            }
                        }
                    }

                }
            }
            return "ok";
        }


        //直接领料
        public static string GetST11(ST_Rdrecord11 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

          

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {


                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT b.cInvCode,b.cComUnitCode,b.cGroupCode,
b.cSTComUnitCode AS cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,b.igrouptype,binvbatch,b.bInvQuality
FROM   dbo.Inventory b  
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON b.cSTComUnitCode=c.cComunitCode
  WHERE b.cinvcode  = '{0}' ", cInvCode);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行存货编码不正确", (i + 1).ToString());
                }
                else
                {

                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);

                    string iGrouptype = DbHelper.GetDbString(dt.Rows[0]["igrouptype"]);
                    //固定换算率和浮动换算率，都是需要件数和辅助计量单位，换算率
                    if (iGrouptype == "1" || iGrouptype == "2")
                    {
                        //固定换算
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                        {
                            rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                        }
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cUnitID"]);
                        rdrecord.bodies[i].inum = DbHelper.GetDbString(DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity) / DbHelper.GetDbdecimal(dt.Rows[0]["iChangRate"]));

                    }

                    //有批次管理的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["binvbatch"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].cbatch))
                        {
                            return rdrecord.bodies[i].cinvcode + "批次没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].cbatch = "";
                    }
                    //有保质期的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["bInvQuality"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].dvdate))
                        {
                            return rdrecord.bodies[i].cinvcode + "失效日期没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].dvdate = "";
                    }

                    if (!string.IsNullOrEmpty(rdrecord.bodies[i].citem_class))
                    {
                        sql = string.Format(@" select citem_class, citem_name, ctable from fitem where citem_class = '{0}'", rdrecord.bodies[i].citem_class);
                        DataTable dtitemclass = DbHelper2.ExecuteTable(sql, CanShu.conStr);
                        if (dtitemclass.Rows.Count > 0)
                        { 
                            rdrecord.bodies[i].citemcname = DbHelper.GetDbString(dtitemclass.Rows[0]["citem_name"]); 
                            string citemtable = DbHelper.GetDbString(dtitemclass.Rows[0]["ctable"]);
                            if (!string.IsNullOrEmpty(rdrecord.bodies[i].citemcode))
                            {
                                sql = string.Format(@"select bclose,cItemName from  {1} where citemcode='{0}'", rdrecord.bodies[i].citemcode, citemtable);
                                DataTable dtitem = DbHelper2.ExecuteTable(sql, CanShu.conStr);
                                if (dtitem.Rows.Count > 0)
                                {

                                    //项目关闭了，只填写大类，不填写明细
                                    if (DbHelper.GetDbInt(dtitem.Rows[0]["bclose"]) == 1)
                                    {
                                        //rdrecord.bodies[i].citem_class = DbHelper.GetDbString(dt.Rows[0]["cItem_class"]);
                                        //rdrecord.bodies[i].citemcname = DbHelper.GetDbString(dt.Rows[0]["citem_name"]);
                                        rdrecord.bodies[i].cbatchproperty8 = "订单项目已关闭";
                                    }
                                    else
                                    {
                                        //研发项目赋值 
                                        rdrecord.bodies[i].cname = DbHelper.GetDbString(dtitem.Rows[0]["cItemName"]); 
                                    }
                                }

                            }
                        }

                        }
                  
                    
                }
            }
            return "ok";
        }



        public static string GetST10ByMOInfo(ST_Rdrecord10 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            { 
                //华进项目，生产订单只有1行
                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT a.InvCode,b.cGroupCode,a.AuxUnitCode ,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,
 mo.MoCode,a.MoDid,a.MDeptCode,a.SoCode,a.SoType,a.MoLotCode  ,a.SortSeq,binvbatch,b.bInvQuality,a.OrderType,a.OrderCode,a.OrderDId,a.OrderSeq,
a.define23 as cItemCode,a.MoTypeId
FROM dbo.mom_orderdetail a 
INNER JOIN mom_order mo ON a.MoId=mo.MoId
  INNER JOIN dbo.Inventory b  ON a.InvCode=b.cInvCode
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON a.AuxUnitCode=c.cComunitCode
  WHERE mo.mocode = '{0}'  and a.SortSeq ='{1}' ", rdrecord.bodies[i].cmocode, rdrecord.bodies[i].imoseq);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                 




                if (DbHelper.GetDbString(dt.Rows[0]["InvCode"]) != rdrecord.bodies[i].cinvcode)
                {
                    return string.Format("第{0}行订单物料和{1}不一致", (i + 1).ToString(), DbHelper.GetDbString(dt.Rows[0]["InvCode"]));
                }
                //写表头信息
                if (i == 0)
                    {


                        rdrecord.head.cdepcode = DbHelper.GetDbString(dt.Rows[0]["MDeptCode"]);
                        rdrecord.head.cmpocode = DbHelper.GetDbString(dt.Rows[0]["MoCode"]);
                        rdrecord.head.iproorderid = DbHelper.GetDbString(dt.Rows[0]["MoDid"]);

                    }
                    rdrecord.bodies[i].impoids = DbHelper.GetDbString(dt.Rows[0]["MoDid"]);
                    //rdrecord10.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]); 
                    rdrecord.bodies[i].cmocode = DbHelper.GetDbString(dt.Rows[0]["MoCode"]);
                    rdrecord.bodies[i].imoseq = DbHelper.GetDbString(dt.Rows[0]["SortSeq"]);
                    rdrecord.bodies[i].iordertype = DbHelper.GetDbString(dt.Rows[0]["OrderType"]);
                    rdrecord.bodies[i].iordercode = DbHelper.GetDbString(dt.Rows[0]["OrderCode"]);
                    rdrecord.bodies[i].iorderdid = DbHelper.GetDbString(dt.Rows[0]["OrderDId"]);
                    rdrecord.bodies[i].iorderseq = DbHelper.GetDbString(dt.Rows[0]["OrderSeq"]);

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["AuxUnitCode"])))
                    {
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["AuxUnitCode"]);

                    }


                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                    {
                        rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                    }
                    //有批次管理的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["binvbatch"]) == 1)
                    {
                        if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].cbatch))
                        {
                            return rdrecord.bodies[i].cinvcode + "批次没有填写";
                        }

                    }
                    else
                    {
                        rdrecord.bodies[i].cbatch = "";
                    }
                    //有保质期的填写，没有的不写
                    if (DbHelper.GetDbInt(dt.Rows[0]["bInvQuality"]) == 1)
                    {
                    if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].dmadedate))
                    {
                        return rdrecord.bodies[i].cinvcode + "生效日期没有填写";
                    }

                    if (string.IsNullOrWhiteSpace(rdrecord.bodies[i].dvdate))
                        {
                            return rdrecord.bodies[i].cinvcode + "失效日期没有填写";
                        }

                    }
                    else
                    {
                    rdrecord.bodies[i].dmadedate = "";
                    rdrecord.bodies[i].dvdate = "";
                    }

                ////增加研发项目取值.MoTypeId = 1000000002， 项目大类默认00
                //if (DbHelper.GetDbString(dt.Rows[0]["MoTypeId"]) == "1000000002")
                //{
                //    sql = string.Format(@"select bclose,cItemName from fitemss00 where citemcode='{0}'", DbHelper.GetDbString(dt.Rows[0]["cItemCode"]));
                //    DataTable dtitem = DbHelper2.ExecuteTable(sql, CanShu.conStr);
                //    if (dtitem.Rows.Count > 0)
                //    {

                //        //项目关闭了，只填写大类，不填写明细
                //        if (DbHelper.GetDbInt(dtitem.Rows[0]["bclose"]) == 1)
                //        {
                //            //rdrecord.bodies[i].citem_class = DbHelper.GetDbString(dt.Rows[0]["cItem_class"]);
                //            //rdrecord.bodies[i].citemcname = DbHelper.GetDbString(dt.Rows[0]["citem_name"]);
                //            rdrecord.bodies[i].cbatchproperty8 = "订单项目已关闭";
                //        }
                //        else
                //        {
                //            //研发项目赋值
                //            rdrecord.bodies[i].citem_class = "00";
                //            rdrecord.bodies[i].citemcname = "研发项目";
                //            rdrecord.bodies[i].cname = DbHelper.GetDbString(dtitem.Rows[0]["cItemName"]);
                //            rdrecord.bodies[i].citemcode = DbHelper.GetDbString(dt.Rows[0]["cItemCode"]);
                //        }
                //    }
                //}



            }
            return "ok";
        }
    }
}
